﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace NWBA_Public

{
    public class Transaction
    {
        public int TransactionID { get; set; }
        public char TransactionType { get; set; }
        public int AccountNumber { get; set; }
        public int DestAccount { get; set; }
        public decimal Amount { get; set; }
        public string Comment { get; set; }
        public DateTime ModifyDate { get; set; }
        public Transaction(char TransType, int acctNum, int DestAcct, decimal Amt, string com, DateTime date)
        {
            this.TransactionType = TransType;
            this.AccountNumber = acctNum;
            this.DestAccount = DestAcct;
            this.Amount = Amt;
            this.Comment = com;
            this.ModifyDate = date;

        }
        public Transaction(int TransID, char TransType, int acctNum, int DestAcct, decimal Amt, string com, DateTime date)
        {
            this.TransactionID = TransID;
            this.TransactionType = TransType;
            this.AccountNumber = acctNum;
            this.DestAccount = DestAcct;
            this.Amount = Amt;
            this.Comment = com;
            this.ModifyDate = date;

        }
    }
    public class TransactionRepository
    {
        public static SqlConnection con = new SqlConnection(ServerString.getServerString());
        public TransactionRepository()
        {
        }
        public static List<Transaction> SelectByCustID(int CustomerID)
        {
            var AcctList = AccountRepostiory.Select(CustomerID);
            if (AcctList.Count != 0)
            {
                List<Transaction> TranList = new List<Transaction>();
                foreach (Account acc in AcctList)
                {
                    List<Transaction> newList = Select(acc);
                    foreach (Transaction trans in newList)
                    {
                        TranList.Add(trans);
                    }
                }
                return TranList;
            }
            return null;
        }
        public static List<Transaction> Select(Account acct)
        {
            return Select(acct.AccountNumber);
        }
        public static List<Transaction> Select(int accountID)
        {
            List<Transaction> _Transaction = new List<Transaction>();
            con.Open();
            SqlCommand command = new SqlCommand("SELECT TransactionID, TransactionType, AccountNumber, DestinationAccount, Amount, Comment, ModifyDate" +
                " FROM [Transaction] WHERE AccountNumber=" + accountID + " OR DestinationAccount=" + accountID, con);
            SqlDataReader sqlr = command.ExecuteReader();
            while (sqlr.Read())
            {
                Transaction newTrans = new Transaction(sqlr.GetInt32(0), sqlr.GetString(1).ToCharArray()[0], sqlr.GetInt32(2), sqlr.GetInt32(3), sqlr.GetDecimal(4), sqlr.GetString(5), sqlr.GetDateTime(6));
                _Transaction.Add(newTrans);
            }
            con.Close();
            return _Transaction;
        }


        private static Boolean chckServiceFee(Account acct)
        {
            if (Select(acct).Count > 4)
            {
                return true;
            }
            return false;
        }
        public static Boolean ServiceFee(Account acct, String comment, Boolean transact)
        {
            //check weather the service fee apply
            if (chckServiceFee(acct) || !transact)
            {
                //if apply create a service fee transaction
                Transaction newTrans = new Transaction('S', acct.AccountNumber, acct.AccountNumber, decimal.Parse("0.2"), comment, DateTime.Now);
                if (Insert(newTrans))
                {
                    if (AccountRepostiory.UpdateWithdraw(acct, decimal.Parse("0.2")))
                    {
                        //service fee apply and has succsfully created a service fee trnsaction for it.
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    //any thing wrong during the proceess just return false
                    return false;
                }
            }
            //if service fee doesn't apply also return true
            return true;
        }

        public static Boolean Insert(Transaction trans)
        {
            //insert a new transaction into database
            SqlCommand command = new SqlCommand("INSERT INTO [Transaction] ([TransactionType],[AccountNumber],[DestinationAccount],[Amount],[Comment],[ModifyDate]) values (@Type, @Num, @DAcct, @Amt, @Com, @date)");
            //specification for each of the insert value
            command.Parameters.AddWithValue("@ID", SqlDbType.Int);

            command.Parameters.AddWithValue("@Type", SqlDbType.NVarChar).Value = trans.TransactionType;

            command.Parameters.AddWithValue("@Num", SqlDbType.Int).Value = trans.AccountNumber;

            command.Parameters.AddWithValue("@DAcct", SqlDbType.Int).Value = trans.DestAccount;

            command.Parameters.AddWithValue("@Amt", SqlDbType.Money).Value = trans.Amount;

            command.Parameters.AddWithValue("@Com", SqlDbType.NVarChar).Value = trans.Comment;

            command.Parameters.AddWithValue("@date", SqlDbType.DateTime).Value = trans.ModifyDate;
            con.Open();
            command.Connection = con;

            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}